package com.lengyu.vxminiprogram.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
*@Description: 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用
*@Param:
*@return:
*@Author: liuyu
*@date: 2019/12/31 15:29
**/
@Component
public class MetaHandler implements MetaObjectHandler {
    /**
     * 新增数据执行
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Date createTime = (Date)metaObject.getValue("createTime");
        if(createTime == null){
            this.setFieldValByName("createTime", new Date(), metaObject);
        }
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    /**
     * 更新数据执行
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

}